package com.heima.kafka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Kafka主题生产者
 */
@RestController
public class ProducerController {

    @Autowired
    private KafkaTemplate kafkaTemplate;


    @GetMapping("/send/{key}/{value}")
    public String sendMsg(@PathVariable("key")String key, @PathVariable("value") String value){
        kafkaTemplate.send("myTopic",0, key, value).addCallback(new ListenableFutureCallback() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }

            @Override
            public void onSuccess(Object o) {
                System.out.println(o);
            }
        });
        System.out.println("生产消息完毕了。。。");
        return "ok";
    }

}
